var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    vehicleData: [],
    searchShowed: false,
    searchVal: "",
    selected: false
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName + "--选择承运商" });
    wx.showToast({
      title: "页面加载中\n请稍候...",
      icon: "loading",
      duration: 5000
    });
    this.loadData();
  },
  onReady: function () {
    wx.hideToast()
  },
  showSearch: function () {
    this.setData({
      searchShowed: true
    });
  },
  hideSearch: function () {
    this.setData({
      searchVal: "",
      searchShowed: false
    });
    this.setCarrierData("");
  },
  clearSearch: function () {
    this.setData({
      searchVal: ""
    });
    this.setCarrierData("");
  },
  searchTyping: function (e) {
    this.setData({
      searchVal: e.detail.value
    });
    this.setCarrierData(e.detail.value);
  },
  loadData() {
    wx.showLoading({
      title: '加载数据中...',
    });
    var that = this;
    util.getData("/atapi/carrier/carrier/carrier", {
      'CO_ID': app.globalData.CO_ID
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (res.data.success) {
        var _data = res.data.data, _data_ = [];
        for (var i = 0; i < _data.length; i++) {
          _data_.push({ name: _data[i].name, carrier_coid: _data[i].carrier_coid, value: _data[i].ID });
        };
        that.setData({
          carrierData: _data_,
          dataList: _data_
        })
      }
    });
  },
  setCarrierData: function (text) {
    var that = this;
    var data = that.data.dataList, _data_ = [];
    for (var i = 0; i < data.length; i++) {
      if (String(data[i].name).indexOf(text) > -1) {
        _data_.push({ name: data[i].name, carrier_coid: data[i].carrier_coid, value: data[i].value });
      }
    };
    that.setData({
      carrierData: _data_
    })
  },
  carrierSelect: function (e) {
    console.log(e)
    var carrierData = this.data.carrierData;
    for (var i = 0; i < carrierData.length; i++) {
      carrierData[i].checked = carrierData[i].value == e.detail.value;
      if (carrierData[i].value == e.detail.value) {
        wx.setStorage({
          key: "path_carrier",
          data: carrierData[i]
        });
        this.setData({
          selected: true
        })
      }
    }
    this.setData({
      carrierData: carrierData
    });
  },
  confirmData: function () {
    if (!this.data.selected) {
      wx.setStorage({
        key: "path_carrier",
        data: ''
      });
    }
    wx.navigateBack({
      delta: 1
    });
  },
  cancelConfirmData: function () {
    wx.navigateBack({
      delta: 1
    });
  }
});